#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 rambla.eu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.import os
import platform, os, sys, datetime
import json
import unittest
import setup_environ
import raws_json
from raws_json.raws_service import RawsService, Feed, Query
from raws_json.meta.service import MetaService
from raws_json.meta.vocab import Vocab

class VocabTest(unittest.TestCase):
    
    @classmethod
    def setUpClass(cls):
        cls.meta_service = MetaService(username = setup_environ.TEST_USERNAME, password = setup_environ.TEST_PASSWORD, server = setup_environ.TEST_SERVER)
        # delete all existing content instances
        feed = cls.meta_service.getVocabList()
        mf = Feed(feed)
        for e in mf.entries:
            cls.meta_service.deleteVocab(e)

    def setUp(self):
        """ Delete all content for the account before running each test. """
        # delete all existing content instances
        feed = self.meta_service.getVocabList()
        mf = Feed(feed)
        for e in mf.entries:
            self.meta_service.deleteVocab(e)
    
    def test_create_vocab(self):
        """ Create a new vocab instance. """
        v = Vocab(name = "test", description = "test vocab", xml_namespace = "http://hallo.vocab.com/test/")
        vocab_entry = self.meta_service.createVocab(v.to_entry())
        v2 = Vocab(entry = vocab_entry)
        # initial content obj must be the same as the one we got back from meta
        self.assertEqual(v, v2)
    
    def test_update_vocab__xml_namespace(self):
        """ Update a vocab instance. """
        # create initial
        v = Vocab(name = "test", description = "test vocab", xml_namespace = "http://hallo.vocab.com/test/")
        vocab_entry = self.meta_service.createVocab(v.to_entry())
        v2 = Vocab(entry = vocab_entry)
        self.assertEqual(v, v2)
        # update the xml_namespace of this vocab
        v2.xml_namespace = "http://hallo.vocab.rambla/test/"
        vocab_entry = self.meta_service.updateVocab(v2.to_entry())
        v3 = Vocab(entry = vocab_entry)
        self.assertEqual(v2, v3)
        self.assertNotEqual(v, v3)
    
    def test_get_vocab_instance(self):
        """ Get a vocab instance. """
        v = Vocab(name = "test", description = "test vocab", xml_namespace = "http://hallo.vocab.com/test/")
        vocab_entry = self.meta_service.createVocab(v.to_entry())
        v2 = Vocab(entry = vocab_entry)
        # get the vocab entry
        vocab_entry = self.meta_service.getVocabInstance(name = v2.name)        
        v3 = Vocab(entry = vocab_entry)
        self.assertEqual(v, v3)
        self.assertEqual(v2, v3)
    
    def test_get_vocab_feed(self):
        """ Get a vocab feed. """
        # vocab 1
        v1 = Vocab(name = "test", description = "test vocab", xml_namespace = "http://hallo.vocab.com/test/")
        vocab_entry = self.meta_service.createVocab(v1.to_entry())
        v1 = Vocab(entry = vocab_entry)
        # vocab 2
        v2 = Vocab(name = "rambla_test", xml_namespace = "http://hallo.rambla.eu/test/")
        vocab_entry = self.meta_service.createVocab(v2.to_entry())
        v2 = Vocab(entry = vocab_entry)
        # get feed
        feed = self.meta_service.getVocabList()
        vf = Feed(feed)
        self.assertEqual(2, len(vf.entries))
        for e in vf.entries:
            v = Vocab(entry = e)
            if unicode(v1.name) == unicode(v.name):
                self.assertEqual(v1, v)
            elif unicode(v2.name) == unicode(v.name):
                self.assertEqual(v2, v)
            else:
                self.assertTrue("test_get_list(): unexpected vocab name = %s" % v.name)
    
    def test_delete_vocab(self):
        """ Delete vocab instance """
        v = Vocab(name = "test", description = "test vocab", xml_namespace = "http://hallo.vocab.com/test/")
        vocab_entry = self.meta_service.createVocab(v.to_entry())
        v2 = Vocab(entry = vocab_entry)
        # delete
        self.meta_service.deleteVocab(v2.to_entry())
        # get feed -> should be empty
        feed = self.meta_service.getVocabList()
        vf = Feed(feed)
        self.assertEqual(0, len(vf.entries))
        
    def test_create_fail__no_vocab_name(self):
        """ Try to create vocab without name -> fail ! """
        failed = False
        v = Vocab(description = "test vocab", xml_namespace = "http://hallo.vocab.com/test/")
        try:
            vocab_entry = self.meta_service.createVocab(v.to_entry())
        except Exception, e:
            failed = True
            # print str(e)
        self.assertTrue(failed)
    
    def test_create__no_xml_namespace(self):
        """ Try to create vocab without namespace -> will be set by META ! """
        failed = False
        v = Vocab(name = "no_namespace", description = "test vocab")
        vocab_entry = self.meta_service.createVocab(v.to_entry())
        v2 = Vocab(entry = vocab_entry)
        self.assertTrue(v2.xml_namespace)
    
    def test_create__name_with_non_ascii_chars(self):
        """ Vocab name should not contains non ascii chars -> stripped from name ! """
        failed = False
        v = Vocab(name = "Ùnicodé", xml_namespace = "http://hallo.vocab.com/test/")
        vocab_entry = self.meta_service.createVocab(v.to_entry())
        v2 = Vocab(entry = vocab_entry)
        self.assertEqual(v2.name, "nicod")
    
    def test_create__name_with_space(self):
        """ Vocab name should not contains spaces -> error returned """
        failed = False
        v = Vocab(name = "name with spaces", xml_namespace = "http://hallo.vocab.com/test/")
        try:
            vocab_entry = self.meta_service.createVocab(v.to_entry())
        except Exception, e:
            failed = True
            # print str(e)
        self.assertTrue(failed)
    
    def test_create__name_with_non_alphanumeric_chars(self):
        """ Vocab name should not contains non alphanumeric chars -> cerror returned """
        failed = False
        v = Vocab(name = "?nic'od>", xml_namespace = "http://hallo.vocab.com/test/")
        try:
            vocab_entry = self.meta_service.createVocab(v.to_entry())
        except Exception, e:
            failed = True
            # print str(e)
        self.assertTrue(failed)

    def test_create_same_vocab_twice(self):
        """ Create a new vocab instance. """
        v = Vocab(name = "test", description = "test vocab", xml_namespace = "http://hallo.vocab.com/test/")
        vocab_entry = self.meta_service.createVocab(v.to_entry())
        v2 = Vocab(entry = vocab_entry)
        # initial content obj must be the same as the one we got back from meta
        self.assertEqual(v, v2)
        failed = False
        try:
            vocab_entry = self.meta_service.createVocab(v.to_entry())
        except Exception, e:
            failed = True
            # print str(e)
        self.assertTrue(failed)
    

def suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(VocabTest))
   return suite

if __name__=='__main__':
    unittest.main()
